package org.one.afternoon.mock;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.one.afternoon.common.Result;
import org.one.afternoon.common.SimpleUtils;
import org.one.afternoon.domain.Client;
import org.one.afternoon.domain.Hospital;
import org.one.afternoon.service.ClientService;
import org.one.afternoon.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/mock")
@Slf4j
public class MockInterface {

    @Autowired
    private ClientService clientService;

    @Autowired
    private HospitalService hospitalService;

    /**
     * 用于响应前端按钮 获取模拟数据并修改数据库Status状态
     *
     * @param index 序号
     * @return 返回是否检测成功
     */
    @CrossOrigin
    @PostMapping("/isHealth")
    @ResponseBody
    public Result<Integer> IsHealth(@RequestParam Integer index) {
        LambdaUpdateWrapper<Client> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        LambdaQueryWrapper<Client> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Client::getId, index);
        int status = RandomValue.oneOrZero();
        if (status == 0) {
            lambdaUpdateWrapper.set(Client::getStatus, 0)
                    .eq(Client::getId, index);
            clientService.update(lambdaUpdateWrapper);
        } else {
            lambdaUpdateWrapper.set(Client::getStatus, 1)
                    .eq(Client::getId, index);
            clientService.update(lambdaUpdateWrapper);
        }
        log.info(String.valueOf(status));
        return Result.success(status);
    }

    /**
     * 用于响应前端按钮 获取模拟数据并返回签名
     *
     * @param index 序号
     * @return 签名
     */
    @CrossOrigin
    @PostMapping("/getCustomersSign")
    @ResponseBody
    public Result<String> getCustomersSign(@RequestParam Integer index) {
        LambdaUpdateWrapper<Client> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        String customerSign=RandomValue.getCustomersSign();
        lambdaUpdateWrapper.eq(Client::getId,index)
                        .set(Client::getCustomersSign,customerSign);
        clientService.update(null,lambdaUpdateWrapper);
        return Result.success(customerSign);
    }

    /**
     * 响应前端请求 用于模拟数据
     *
     * @return 返回所有数据
     */
    @CrossOrigin // 跨域访问
    @GetMapping("/all")
    @ResponseBody
    public Result<List<MockInterface.HospitalAndClient>> findAll() {
        List<MockInterface.HospitalAndClient> hospitalAndClientList = new ArrayList<>();
        List<Client> clientlist = clientService.list(null);
        List<Hospital> hospitalList = hospitalService.list(null);
        for (int i = 0; i < hospitalList.size(); i++) {
            MockInterface.HospitalAndClient hospitalAndClient = new MockInterface.HospitalAndClient();
            hospitalAndClient.setIpAddress(clientlist.get(i).getIpAddress());
            hospitalAndClient.setName(hospitalList.get(i).getName());
            //模拟数据返回
            hospitalAndClient.setStatus(clientlist.get(i).getStatus());
            try {
                hospitalAndClient.setCustomersSign(clientlist.get(i).getCustomersSign());
            } catch (Exception e) {
                e.printStackTrace();
            }
            hospitalAndClientList.add(hospitalAndClient);
        }
        return Result.success(hospitalAndClientList);
    }

    /**
     * 用于响应前端按钮 获取模拟数据并返回Info信息
     * @param name 名字
     * @param idCard 身份证
     * @param ip ip地址
     * @return Info信息
     */
    @CrossOrigin
    @PostMapping("/getCustomersInfo")
    @ResponseBody
    public Result<String> getCustomersInfo(@RequestParam String name, @RequestParam String idCard, @RequestParam String ip) {
        int result = RandomValue.oneOrZero();
        if(!SimpleUtils.IsNotBlank(name,idCard,ip)){
            return Result.error("数据缺失");
        }
        List<Client> list = clientService.list(new LambdaQueryWrapper<Client>().eq(StringUtils.isNotBlank(ip), Client::getIpAddress, ip));
        Hospital hospital = hospitalService.getById(list.get(0).getHospitalId());
        if(result == 1){
            return Result.success(String.format("姓名:%s,身份证:%s,所属医院:%s",name,idCard,hospital.getName()));
        }else{
            return Result.error("查询失败");
        }
    }

    @Data
    private class HospitalAndClient {
        private String name;
        private String ipAddress;
        private Integer status;
        private String customersSign;
    }
}
